package com.foo.ui_demo_widget;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.foo.ui_demo_widget.adapter.ClassroomBuildingAdapter;
import com.foo.ui_demo_widget.pojo.ClassroomBuilding;

import java.util.ArrayList;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        //数据Data《--》数据适配器（Adapter）《--》界面（Spinner）

        //region 学期下拉框：Spinner加载静态数据
        Spinner spSemester = findViewById(R.id.sp_semester);
        spSemester.setSelection(0,true);
        spSemester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0 ) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion

        //region 学院下拉框：Spinner通过ArrayAdapter加载动态数据,使用系统预定义下拉列表项视图
        String[] school = {"-==选择学院==-","计算机学院","文传学院","理学院"};
        Spinner spSchool = findViewById(R.id.sp_school);
        spSchool.setSelection(0,true);
        ArrayAdapter<String> schoolAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,school);
        spSchool.setAdapter(schoolAdapter);
        spSchool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0 ) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion

        //region 年级下拉框：Spinner通过ArrayAdapter加载动态数据，使用自定义下拉列表项视图
        String[] grades = {"-==选择年级==-","大一","大二","大三","大四"};
        Spinner spGrade = findViewById(R.id.sp_grade);
        ArrayAdapter<String> gradeAdapter = new ArrayAdapter<>(this,R.layout.my_spinner_item,grades);
        gradeAdapter.setDropDownViewResource(R.layout.my_spinner_item_dropdown);
        spGrade.setAdapter(gradeAdapter);
        spGrade.setSelection(0,true);
        spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0 ) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion

        //region 教学楼下拉框：Spinner通过自定义适配器加载动态数据，使用自定义下拉列表项视图
        ArrayList<ClassroomBuilding> classroomBuildings = new ArrayList<>();
        classroomBuildings.add(new ClassroomBuilding("-==选择教室==-","",""));
        classroomBuildings.add(new ClassroomBuilding("文友楼","510","多媒体"));
        classroomBuildings.add(new ClassroomBuilding("香远楼","618","普通"));
        classroomBuildings.add(new ClassroomBuilding("实验楼","605","计算机实验室"));
        classroomBuildings.add(new ClassroomBuilding("文友楼","619","多媒体"));
        Spinner spClassroomBuilding = findViewById(R.id.sp_classroom_building);
        spClassroomBuilding.setAdapter(new ClassroomBuildingAdapter(this,classroomBuildings));
        spClassroomBuilding.setSelection(0,true);
        spClassroomBuilding.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0 ) return;
                Toast.makeText(SpinnerActivity.this,
                        parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //endregion
    }
}



